<template>
	<view class="class-student-container">
		<cc-navbar :title="navbarTitle"></cc-navbar>
		<view class="list" v-if="count">
			<view class="item" v-for="(row, index) in list" :key="index" @tap="handleDetail(row.student_id)">
				<view class="item-top fcb">
					<view class="item-top-left fc">
						<view class="u-line-1">
							{{ row.student_name }}
						</view>
						<view class="ml10" v-if="row.sex">
							<u-icon bold :name="row.sex === 2 ? 'woman' : 'man'"
								:color="row.sex === 2 ? '#d54941' : '#0052d9'" size="36rpx"></u-icon>
						</view>
					</view>
					<block v-if="row.statusData">
						<u-tag shape="circle" size="mini" plain plainFill :type="row.statusData.type"
							:text="row.statusData.label"></u-tag>
					</block>
				</view>
				<view class="item-content">
					<view class="item-info">
						<view class="font-info">联系方式:</view>
						<view class="u-line-1 cc-flex-1">{{ row.contact_phone }}({{ row.contactRelation }})</view>
					</view>
					<view class="item-info">
						<view class="font-info">报读课程:</view>
						<div class="cc-flex-1 cc-flex">
							<view>{{ row.course_name }},</view>
							<view class="cc-m-l-8 font-error">剩余{{ row.balance_hour }}{{ row.modeLabel }}</view>
						</div>
					</view>
				</view>
			</view>
			<cc-loadmore :loading="loading" :current="list.length" :count="count" />
		</view>
		<cc-empty v-if="!loading && !count" mode="list" />
	</view>
</template>

<script>
	import {
		getStudentCourse
	} from '@/codecat/api/education/student/course';
	import {
		getStudentStatus,
		getRelation
	} from '@/codecat/config';
	export default {
		name: 'ClassStudent',
		data() {
			return {
				navbarTitle: '',
				loading: false,
				list: [],
				count: 0,
				query: {
					student_name: null,
					cla_id: '',
					page: 1,
					limit: 10,
					group: 'student_id', // 接口必填参数
					status: [1, 2]
				}
			};
		},
		onLoad() {
			const options = this.$Route.query;
			if (options.cla_id) {
				this.query.cla_id = options.cla_id
				this.getList(1);
			}
		},
		onReachBottom() {
			if (this.list.length < this.count) {
				this.getList();
			}
		},
		methods: {
			getList(first) {
				const query = this.$lodash.cloneDeep(this.query);
				if (first) {
					query.page = 1;
					this.list = [];
					this.count = 0;
				} else {
					query.page += 1;
				}
				this.loading = true;
				getStudentCourse(query)
					.then((res) => {
						this.loading = false;
						this.query.page = query.page;
						const {
							list,
							count
						} = res.data;
						this.count = count || 0;
						if (Array.isArray(list) && list.length) {
							this.$lodash.forEach(list, (e) => {
								e.statusData = getStudentStatus(e.status);
								e.contactRelation = getRelation(e.contact_relation);
								e.modeLabel = e.mode === 'hour' ? '课时' : '天';
							});
							this.list = this.list.concat(list);

							if (query.page === 1) {
								this.navbarTitle = `${this.list[0].cla_list[0].class_name}-学员列表`;
							}
						}
					})
					.catch((_) => {
						this.loading = false;
					});
			},
			handleDetail(id) {
				this.$Router.push({
					path: '/student-detail',
					query: {
						id
					}
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
	@import './index.scss';
</style>